home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Sep / di9809cd / WPTelevision.DPR < prev    next >
Text File  |  1997-12-23  |  15KB  |  476 lines

  1. Library WPTelevision;
  2.  
  3. uses
  4.   SysUtils,
  5.   Windows,
  6.   Registry,
  7.   ShellAPI,
  8.   sharedmemory in 'SharedMemory.pas',
  9.   WinPerf in 'WinPerf.pas',
  10.   TelevisionLIB in 'TelevisionLIB.pas';
  11.  
  12. //
  13. // SMARTBroker data structures
  14. //
  15. Const
  16.   OpenCount : Integer = 0;       // count of "Open" threads
  17.   NumberOfCounters : Integer = 6;
  18.   DefaultCounter : Integer = 2;
  19.   DLLInitOK : Boolean = False;   // true = DLL initialized OK
  20.   ObjectCounter = Television;
  21.   ObjectName : String = 'Television';
  22.   SharedMemName : String = 'Television_SMEM';
  23.   DLLName : String = 'WPTelevision.DLL';
  24.   ININame : String = 'WPTelevision.INI';
  25.   FirstActualCounter = TelevisionPower;
  26.  
  27. //
  28. // The OUTPUT_COUNTERS structure gives the layout of counters in
  29. // the performance output structure.
  30. //
  31. type
  32.   PPointer = ^Pointer;
  33.   POUTPUT_COUNTERS = ^TOUTPUT_COUNTERS;
  34.   TOUTPUT_COUNTERS = record
  35.     Power : LongInt;
  36.     Channel : LongInt;
  37.     ClosedCaptioningEnabled : LongInt;
  38.     SleepTimer : LongInt;
  39.     TimeOn : LongInt;
  40.     ClosedCaptioningWordsPerSecond : LongInt;
  41.   end;
  42.  
  43. var
  44.   FirstCounter : Cardinal;  // The first counter offset from the registry
  45.   FirstHelp : Cardinal;     // The first help offset from the registry.
  46.   SharedMem : TSharedMem;
  47.   ACCESS_SMEM : PTELEVISION_SMEM;
  48.   pOutputStructure : LPDWORD;
  49.  
  50. function GetSizeOfSpecificCounter(CounterNum : Integer) : Integer;
  51. begin
  52.   Result:=0;
  53.   case CounterNum of
  54.     0 : Result:=SizeOf(DWORD);
  55.     1 : Result:=SizeOf(DWORD);
  56.     2 : Result:=SizeOf(DWORD);
  57.     3 : Result:=SizeOf(DWORD);
  58.     4 : Result:=SizeOf(DWORD);
  59.     5 : Result:=SizeOf(DWORD);
  60.   end;
  61. end;
  62.  
  63. function GetSizeOfAllCounters : Integer;
  64. var
  65.   i : Integer;
  66. begin
  67.   Result:=0;
  68.   for i:=0 to NumberOfCounters-1 Do Result:=Result+GetSizeofSpecificCounter(i);
  69. end;
  70.  
  71. function OpenData(lpDeviceNames : PWideChar) : DWord; stdcall;
  72. var
  73.   Registry : TRegistry;
  74.   keystr : String;
  75. begin
  76.    /////////////////////////////////////////////////////////
  77.    //Routine Description:
  78.    //
  79.    //    This routine will open and map the memory used by the SMART object to
  80.    //    pass performance data.
  81.    //
  82.    //Arguments:
  83.    //
  84.    //    Pointer to object ID of each device to be opened.
  85.    //
  86.    //
  87.    //Return Value:
  88.    //
  89.    //    None.
  90.    /////////////////////////////////////////////////////////
  91.  
  92.    //
  93.    //  Since SCREG is multi-threaded and will call this routine in
  94.    //  order to service remote performance queries, this library
  95.    //  must keep track of how many times it has been opened (i.e.
  96.    //  how many threads have accessed it). the registry routines will
  97.    //  limit access to the initialization routine to only one thread
  98.    //  at a time so synchronization (i.e. reentrancy) should not be
  99.    //  a problem
  100.    //
  101.   if (OpenCount=0) then
  102.   begin
  103.     // open shared memory used by object to pass performance values
  104.     SharedMem:=TSharedMem.Access(SharedMemName);
  105.     if (SharedMem.Buffer<>nil) then
  106.     begin
  107.       ACCESS_SMEM := SharedMem.Buffer;
  108.       // get counter and help index base values from registry
  109.       //      Open key to registry entry
  110.       //      read First Counter and First Help values
  111.       Registry:=TRegistry.Create;
  112.       Registry.RootKey:=HKEY_LOCAL_MACHINE;
  113.       keystr:='SYSTEM\CurrentControlSet\Services\'+ObjectName+'\Performance';
  114.       if (not Registry.OpenKey(keystr,False)) then
  115.       begin
  116.         // this is fatal, if we can't get the base values of the
  117.         // counter or help names, then the names won't be available
  118.         // to the requesting application  so there's not much
  119.         // point in continuing.
  120.         Result:=GetLastError;
  121.         Registry.CloseKey;
  122.         Exit;
  123.       end;
  124.       if (not Registry.ValueExists('First Counter')) then
  125.       begin
  126.         // this is fatal, if we can't get the base values of the
  127.         // counter or help names, then the names won't be available
  128.         // to the requesting application  so there's not much
  129.         // point in continuing.
  130.         Result:=GetLastError;
  131.         Registry.CloseKey;
  132.         Exit;
  133.       end else
  134.         FirstCounter:=Registry.ReadInteger('First Counter');
  135.       if (not Registry.ValueExists('First Help')) then
  136.       begin
  137.         // this is fatal, if we can't get the base values of the
  138.         // counter or help names, then the names won't be available
  139.         // to the requesting application  so there's not much
  140.         // point in continuing.
  141.         Result:=GetLastError;
  142.         Registry.CloseKey;
  143.         Exit;
  144.       end else
  145.         FirstHelp:=Registry.ReadInteger('First Help');
  146.       Registry.CloseKey;
  147.       DLLInitOK:=True;
  148.       Registry.Free;
  149.     end else
  150.     begin
  151.       Result:=GetLastError;
  152.       Exit;
  153.     end;
  154.   end;
  155.   Inc(OpenCount);
  156.   Result:=ERROR_SUCCESS;
  157. end;
  158.  
  159. function CloseData : DWord; stdcall;
  160. begin
  161.   ////////////////////////////////////////////////////////
  162.   //
  163.   //Routine Description:
  164.   //
  165.   //    This routine closes the open handles to SMART
  166.   //    device performance counters
  167.   //
  168.   //Arguments:
  169.   //
  170.   //    None.
  171.   //
  172.   //Return Value:
  173.   //
  174.   //    ERROR_SUCCESS
  175.   //
  176.   /////////////////////////////////////////////////////
  177.   Dec(OpenCount);
  178.   if (OpenCount=0) then
  179.   begin
  180.     SharedMem.Free;
  181.     if (pOutputStructure^<>0) then
  182.     begin
  183.       FreeMem(pOutputStructure,SizeOf(Integer));
  184.       pOutputStructure:=nil;
  185.     end;
  186.     SharedMem:=nil;
  187.   end;
  188.   Result:=ERROR_SUCCESS;
  189. end;
  190.  
  191. (************************************************************************)
  192.  
  193. procedure InitializeOutputStructure;
  194. var
  195.   SpaceNeeded : Cardinal;
  196.   i,j,total : Integer;
  197.   pOutput : LPDWORD;
  198. begin
  199.   // Allocate and initialize the response buffer.
  200.   if (pOutputStructure<>nil)then Exit;  //nothing to do
  201.   SpaceNeeded:=sizeof(TPERF_OBJECT_TYPE) + sizeof(TPERF_COUNTER_BLOCK);
  202.   for i:=0 to NumberOfCounters-1 do
  203.   SpaceNeeded:=SpaceNeeded +
  204.                sizeof(TPERF_COUNTER_DEFINITION) +
  205.                GetSizeOfSpecificCounter(i);
  206.   GetMem(pOutputStructure,SpaceNeeded);
  207.   pOutput:=pOutputStructure;
  208.   //
  209.   // The following lines initialize the PERF_OBJECT_TYPE structure.
  210.   //
  211.   // Set the index to the next object
  212.   pOutput^:=DWORD(SpaceNeeded);
  213.   Inc(pOutput);
  214.   // Set the index to the first counter block.
  215.   pOutput^ := DWORD(SpaceNeeded-SizeOf(TPERF_COUNTER_BLOCK)-GetSizeOfAllCounters);
  216.   Inc(pOutput);
  217.   // Set index to first counter
  218.   pOutput^:=DWORD(SizeOf(TPERF_OBJECT_TYPE));
  219.   Inc(pOutput);
  220.   // Set object name index.
  221.   pOutput^:=DWORD(FirstCounter+ObjectCounter);
  222.   Inc(pOutput);
  223.   // Null out the object name title
  224.   pOutput^:=DWord(0);
  225.   Inc(pOutput);
  226.   // Set object help index.
  227.   pOutput^:=DWORD(FirstHelp+ObjectCounter);
  228.   Inc(pOutput);
  229.   // Null out the object help title
  230.   pOutput^:=DWord(0);
  231.   Inc(pOutput);
  232.   // Set the detail level
  233.   pOutput^:=DWORD(PERF_DETAIL_ADVANCED);
  234.   Inc(pOutput);
  235.   // Set the number of counters.
  236.   pOutput^:=DWORD(NumberOfCounters);
  237.   Inc(pOutput);
  238.   // Set the default counter
  239.   pOutput^:=DWORD(DefaultCounter);
  240.   Inc(pOutput);
  241.   // Set no instances.
  242.   pOutput^:=DWORD(PERF_NO_INSTANCES);
  243.   Inc(pOutput);
  244.   // Indicate the code page; in this case Unicode strings
  245.   pOutput^:=DWORD(0);
  246.   Inc(pOutput);
  247.   // Pad out PerfTime
  248.   pOutput^:=DWORD(0);
  249.   Inc(pOutput);
  250.   pOutput^:=DWORD(0);
  251.   Inc(pOutput);
  252.   // Pad out PerfFreq
  253.   pOutput^:=DWORD(0);
  254.   Inc(pOutput);
  255.   pOutput^:=DWORD(0);
  256.   Inc(pOutput);
  257.   //
  258.   // The following loop initializes the PERF_COUNTER_DEFINITION structures.
  259.   //
  260.   for i:=0 to NumberOfCounters-1 do
  261.   begin
  262.     // Set index to next counter definition.
  263.     pOutput^:=DWORD(SizeOf(TPERF_COUNTER_DEFINITION));
  264.     Inc(pOutput);
  265.     // Set counter name index
  266.     pOutput^:=DWORD(FirstActualCounter + FirstCounter + i + i);
  267.     Inc(pOutput);
  268.     // Null out counter name title
  269.     pOutput^:=DWord(0);
  270.     Inc(pOutput);
  271.     // Set counter help index
  272.     pOutput^:=DWORD(FirstActualCounter + FirstHelp + i + i);
  273.     Inc(pOutput);
  274.     // Null out counter help title
  275.     pOutput^:=DWord(0);
  276.     Inc(pOutput);
  277.     //